using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using EP.Base.BaseService.Manager.Config;
using EP.Base.BaseService.Manager.Dal.Helper;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Dal
{    
	/// <summary>
    /// EsCluster 
    /// </summary>
    public class EsClusters:IEsClusters
    {

		/// <summary>
		/// 添加EsCluster
		/// </summary>
		/// <param name="escluster"></param>
		/// <returns></returns>
		public int Add(EsCluster escluster)
		{
			string sql = @"INSERT INTO [EsCluster]
			([Name],[ServiceHost],[CreateTime])
			VALUES
			(@Name,@ServiceHost,@CreateTime) 
			SELECT SCOPE_IDENTITY()
			";
			var par = new DynamicParameters();
			par.Add("@Name",escluster.Name , DbType.AnsiString);
			par.Add("@ServiceHost",escluster.ServiceHost , DbType.AnsiString);
			par.Add("@CreateTime",escluster.CreateTime , DbType.DateTime);
			return DapWrapper.InnerQueryScalarSql<int>(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 修改EsCluster
		/// </summary>
		/// <param name="escluster"></param>
		/// <returns></returns>
		public int Update(EsCluster escluster)
		{
			string sql = @"UPDATE  [EsCluster] SET 
						Name=@Name,
						ServiceHost=@ServiceHost,
						CreateTime=@CreateTime
 WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", escluster.Id, DbType.Int32);
			par.Add("@Name",escluster.Name , DbType.AnsiString);
			par.Add("@ServiceHost",escluster.ServiceHost , DbType.AnsiString);
			par.Add("@CreateTime",escluster.CreateTime , DbType.DateTime);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据id获取EsCluster
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public EsCluster Get(int id)
		{
			string sql = "select [Id],[Name],[ServiceHost],[CreateTime] FROM [EsCluster] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerQuerySql<EsCluster>(DbConfig.EpBaseServiceManagerConnectionString, sql, par).FirstOrDefault();
		}
		/// <summary>
		/// 根据id删除EsCluster
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Int32 Delete(int id)
		{
			string sql="DELETE [EsCluster] WHERE Id=@Id";
			var par = new DynamicParameters();
			par.Add("@Id", id, DbType.Int32);
			return DapWrapper.InnerExecuteSql(DbConfig.EpBaseServiceManagerConnectionString, sql, par);
		}
		/// <summary>
		/// 根据ids删除EsCluster多条记录
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public Int32 Deletes(int[] ids)
		{
			if (ids.Length == 0)
                return 0;
			string sql="DELETE [EsCluster] WHERE Id IN ("+string.Join(",",ids)+")";
			return DapWrapper.InnerExecuteText(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
		/// <summary>
       /// 获取EsCluster分页列表
       /// </summary>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">每页显示条数</param>
       /// <param name="beginTime">开始时间</param>
       /// <param name="endTime">结束时间</param>
       /// <param name="recordCount">总记录数</param>
       /// <returns>EsCluster列表</returns>
       public List<EsCluster> GetPageList(int pageIndex, int pageSize, DateTime? beginTime, DateTime? endTime, out int recordCount)
       {
           recordCount = 0;
           var par = new DynamicParameters();
           par.Add("@PageIndex", pageIndex, DbType.Int32);
           par.Add("@PageSize", pageSize, DbType.Int32);
           par.Add("@BeginTime", beginTime, DbType.DateTime);
           par.Add("@EndTime", !endTime.HasValue ? endTime : endTime.Value.AddDays(1).AddMilliseconds(-1), DbType.DateTime);
           par.Add("@TotalCount", recordCount, DbType.Int32, ParameterDirection.Output);
           var result = DapWrapper.InnerQueryProc<EsCluster>(DbConfig.EpBaseServiceManagerConnectionString, "proc_GetEsClusterPageList", par);
           recordCount = par.Get<int>("@TotalCount");
           return result;
       }
	   /// <summary>
		/// 获取所有EsCluster
		/// </summary>
		/// <returns></returns>
		public List<EsCluster> GetAll()
		{
			string sql = "select [Id],[Name],[ServiceHost],[CreateTime] FROM [EsCluster]";
			
			return DapWrapper.InnerQuerySql<EsCluster>(DbConfig.EpBaseServiceManagerConnectionString, sql);
		}
    }
}
